/*
 * @Description: 模板相关数据
 */
import { defineStore } from 'pinia';
import pinia from '@/store';
import { useBaseInfoStore } from './base-info';
import globalData from '@/plugins/data';
import globalMethods from "@/plugins/methods";

export const useTemplateStore = defineStore('template', {
    state: () => ({
        isIframeReady: false,
        //iframe对象
        zwIframe: {},
        //选择模板的iframe的url
        zwIframeUrl: '',
        //选择模板的iframe的url，备份。暂时只用于相关数据拼接CAP3表单
        zwIframeUrlCopy: '',
    }),
    actions: {
        /**
         * @description: 设置iframe表单url
         * @param {Object}data url需要的相关数据
         */
        setZWIframeUrl(data) {
            let url = '';
            const nodePolicy = useBaseInfoStore().businessData.nodePolicy||{};
            let actionList = [...(nodePolicy.advanceActionList || []), ...(nodePolicy.commonActionList || [])];
            const  canSign=actionList.includes('Sign');
            const  canEdit=actionList.includes('Edit');
            //是否cap4正文：true：走 cap4，false：走mainbody
            if (data.isCap4) {
                //暂时这样写，等接口好了再改
                const { isSummary } = globalData;
                const {
                    //cap4新建类型browse、create
                    type,
                    //权限id
                    rightId,
                    //协同id
                    moduleId,
                    //affair的subState
                    subState,
                    //是否子流程
                    isSubFlow,
                    //协同模块
                    moduleType,
                } = data.cap4;
                //判断是否加锁
                
                const {alertLockMsg,hasFormLock} = useBaseInfoStore().lockParam;
                let formOpenType = hasFormLock === true ? 'browse' : type;
                url = `${_ctxPath}/common/cap4/template/display/pc/form/dist/index.html?_r=${Date.now()}&type=${formOpenType}&rightId=${rightId}&moduleId=${moduleId}&isSubFlow=${isSubFlow}&moduleType=${moduleType}&affairId=${globalData.affairId}&hasSignAction=${canSign}`;
                if (isSummary) {
                    url += `&nodeName=${encodeURIComponent(
                        useBaseInfoStore().businessData.affair.nodeName
                    )}`;
                } else {
                    url += `&subState=${subState}`;
                }
                if (hasFormLock===true){
                    globalMethods.warning({
                        title: $.i18n('system.reminder.title'),
                        content: alertLockMsg,
                        closable: true,
                        zIndex: 9999
                    });
                }
            } else {
                //表单正文或普通正文(自由协同)
                const {
                    //是否是新建
                    isNew,
                    //协同id
                    moduleId,
                    //协同模块
                    moduleType,
                    //权限id
                    rightId,
                    //是否子流程
                    isSubFlow,
                    //正文类型
                    contentType,
                    //正文内容或正文id
                    transOfficeId,
                    //affair的subState
                    subState,
                    //表单查看状态：1：可编辑，2：只读
                    viewState,
                    //是否已发列表的重复发起
                    resend,
                } = data.mainbody;
                if (!globalData.isSummary){
                    //新建
                    url = `${_ctxPath}/content/content.do?method=index&isFullPage=true&formpage=newcol&isNew=${isNew}&moduleId=${moduleId}&moduleType=${moduleType}&hasSignAction=${canSign}&rightId=${rightId}&isSubFlow=${isSubFlow}&contentType=${contentType}&originalNeedClone=true&transOfficeId=${transOfficeId}&fromIframeName=newCollaboration&subState=${subState}&fullScreen=true&viewState=${viewState}${
                      resend ? '&resend=true' : ''
                    }&rnd=${Date.now()}`;
                }else {
                    
                    let affair = useBaseInfoStore().businessData.affair;
                    let hasDealArea = useBaseInfoStore().businessData.other.hasDealArea;
                    let signatrueShowFlag = !!((affair.state === 3) && "listPending"=== globalData.openFrom);
                    const {canPrint} = useBaseInfoStore().businessData.nodePolicy||{};
                    //处理
                    url = `${_ctxPath}/content/content.do?method=index&isFullPage=true&hasDealArea=${hasDealArea}&moduleId=${moduleId}&moduleType=${moduleType}&hasSignAction=${canSign}&rightId=${rightId}&contentType=${contentType}&viewState=${viewState}&openFrom=${globalData.openFrom}&canDeleteISigntureHtml=${signatrueShowFlag}&isSubFlow=${isSubFlow}&isShowMoveMenu=${signatrueShowFlag}&isShowDocLockMenu=
                    ${signatrueShowFlag}&fromIframeName=summary&nodeName=${encodeURIComponent(affair.nodeName)}&officecanPrint=${canPrint}&canEditContent=${canEdit}&rnd=${Math.random()}${CsrfGuard.getUrlSurffix()}`
                }
                
            }
            this.zwIframeUrl = url || process.env.VUE_APP_IFRAME_URL;
            this.zwIframeUrlCopy = this.zwIframeUrl;
        },
    },
});

//在vue上下文之外使用
export function useTemplateWithOut() {
    return useTemplateStore(pinia);
}
